import { Point2D } from '../point'
import { Shape } from '.'
import { computeBoundingRect } from '../helper/shape'

export interface ICircleProps {
  radius: number
  center: Point2D
  fillColor?: string
}

export default class Circle extends Shape {
  radius: number
  center: Point2D
  fillColor: string

  constructor(props: ICircleProps) {
    super()
    this.radius = props.radius
    this.center = props.center
    this.fillColor = props.fillColor || 'black'
  }

  draw(ctx: CanvasRenderingContext2D) {
    this.boundingRect = computeBoundingRect(this)
    ctx.beginPath()
    ctx.save()
    ctx.fillStyle = this.fillColor
    ctx.arc(this.center.x, this.center.y, this.radius, 0, Math.PI * 2)
    ctx.fill()
    ctx.closePath()
    ctx.restore()
  }

  isPointInClosedRegion(point: Point2D): boolean {
    return ((this.center.x - point.x) ** 2 + (this.center.y - point.y) ** 2) ** 0.5 < this.radius
  }

  setPosition(position: Point2D): void
  setPosition(x: number, y: number): void
  setPosition(x: any, y?: any): void {
    if (x instanceof Point2D || Object.prototype.toString.call(x) === '[object Object]') {
      if (x.hasOwnProperty('x') && x.hasOwnProperty('y')) {
        this.center = x
        Shape.update = this.id
        return
      }
    } else {
      if (typeof x === 'number') {
        this.center = new Point2D(x, y)
        Shape.update = this.id
      }
    }
  }
}
